import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 				= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
const CalibrationModal 	= $Lazy.state(()=>import('./cpt/calibration-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '设备编号',		name: 'device_number' },
			{ label: '设备名称',		name: 'device_id',	type: 'select', loading:true,  width:250 },
			{ label: '周期类型',		name: 'cycle_type',	type: 'select',  idStr:'code' },
		],
		// 提交表单列表
		formData : [
			{ label: '设备名称',		name: 'device_id',		half:true, type: 'select', required:true },
			{ label: '周期值',		name: 'type_val', 		half:true, required:true, type:'number', },
			{ label: '校准周期类型',	name: 'cycle_type',		half:true, type: 'select', required:true, idStr:'code' },
			{ label: '操作人',		name: 'operator_user', 	half:true, required:true, },
			{ label: '提前提醒天数',	name: 'remind_set', 	half:true, required:true, type:'number' },
			{ label: '校准机构',		name: 'agency', 		half:true },
			{ label: '设备注册证号',	name: 'registration_number', half:true },
			{ label: '结果',        name:'content', 		full:true,         type:'textarea' },
		],
	}
	
	model = {}
	
	componentDidMount(){
		const { uuid } = $fn.query()
		if(uuid){ 
			this.query = { device_id: uuid }
			this.uuid = uuid
		}
		
		if(window.isInit){
			this.fetch()
		}
	}
	
	componentWillUnmount(){
		this.uuid = null
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms, formData } = this.state
		
		$cache.then( async f => {
			// 设备名称
			this.deviceData = $fn.setRows([ forms, formData ], 'device_id', await f.getDivCache() )
			// 设置设备默认值
			this.searchFormRef.setValue({ device_id: this.uuid })
			// 周期类型
			$fn.setRows([ forms, formData ], 'cycle_type', await f.getDicCache( { code:44200 } ) )
			//
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'device-plan/index')
	
	// 表单
	cols = [
		{ title: '设备编号',			dataIndex: 'device_number', 	width:100 },
		{ title: '设备名称', 		dataIndex: 'device_name', 		width:150 },
		// 新增设备注册证号字段 20211011 耿琰
		{ title: '设备注册证号', 	dataIndex: 'registration_number', 	width:100 },
		{ title: '上次校准日期', 	dataIndex: 'last_time', 		...$fn.table.time },
		{ title: '下次校准日期', 	dataIndex: 'next_time', 		...$fn.table.time },
		{ title: '校准周期类型', 	dataIndex: 'cycle_type_name', 	width:150 },
		{ title: '实际完成时间', 	dataIndex: 'update_at', 		...$fn.table.time },
		{ title: '校准机构', 		dataIndex: 'agency', 			width:150 },
		{ title: '操作人员', 		dataIndex: 'operator_user', 	...$fn.table.name },
		{ title: '操作时间', 		dataIndex: 'created_at', 		...$fn.table.time },
		TableControls.controls.call(this, { width:300 })
	]
	// 表格操作按钮
	table_controls = (text,rows) => {
		const { uuid, device_id } = rows
		return [
			TableControls.edit.call(this, 'device-plan/info', { uuid }),			// 编辑
			this.onDel(uuid),														// 删除
			TableControls.click.call(this,'校准', ()=> this.calibrationModal.open(rows, this.deviceData) ),
			TableControls.pushTo.call(this,'校准记录', `calibration-record?uuid=${device_id}`),
		]
	}
	// 操作按钮
	controls = () => [
		// 新增文件导入按钮 20211009 耿琰 
		// 文件导入
		Controls.imports.call(this,'device-plan/import', {op_key: 'EquipmentStandardExcel', reconfirm: true}), 
		Controls.add.call(this,{ click: ()=>{
			this.uuid && this.formRef.setValue({ device_id: this.uuid })
		} }),				// 添加
		this.uuid && Controls.back.call(this)	// 返回
	]
	// 删除
	onDel = uuid => Delete.call(this, 'device-plan/delete', { uuid, isKeys: !uuid })
	
	render(){
		return (
			<>
				<Page title='设备校准计划' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, { onForm:this.getSelect }) }
					{/* 表格 */}
					{ Table.call(this,{isInit:window.isInit}) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ width:700, labelWidth:'s7', api: 'device-plan/add', editApi:'device-plan/edit' }) }
					{/* 校准 */}
				<CalibrationModal onRef={ref=>this.calibrationModal=ref} onRefresh={this.fetch} />
			</>
		)
	}
}

export default Index